package com.frxuexi.base.net

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitUtil {
    private val clientToken by lazy {
        OkHttpClient.Builder()
            .readTimeout(30,TimeUnit.SECONDS)
            .connectTimeout(30,TimeUnit.SECONDS)
            .writeTimeout(30,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor(TokenInterceptor())
            .build()
    }

    val retrofitToken by lazy {
        Retrofit.Builder()
            .baseUrl(Const.BASE_URL)
            .client(clientToken)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    val retrofitNews by lazy {
        Retrofit.Builder()
            .baseUrl(Const.BASE_URL_NEWS)
            .client(clientToken)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
}

inline fun <reified T> getServiceToken()= RetrofitUtil.retrofitToken.create(T::class.java)
inline fun <reified T> getServiceNews()= RetrofitUtil.retrofitNews.create(T::class.java)